package com.pti.usermanager.web.controller;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.pti.usermanager.dao.po.DeptPo;
import com.pti.usermanager.service.api.DeptService;
import com.pti.usermanager.web.dto.request.SaveDeptRequest;
import com.pti.usermanager.web.dto.request.SavePostByDidAndPid;
import com.pti.usermanager.web.dto.request.SaveUserByDidAndUid;
import com.pti.usermanager.web.dto.request.UpdateDeptRequest;
import com.pti.usermanager.web.dto.response.ResultResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName: DeptController
 * @Author: liumengyao
 * @Description: 部门控制层
 * @Date: 2019/1/17
 */
@RestController
@Api(value = "部门管理接口", tags = "提供部门的管理接口")
public class DeptController {

    private static final Logger logger = LogManager.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;


    @GetMapping("/dept/getDeptTree")
    @ApiOperation(value = "获取部门列表", notes = "获取部门列表", produces = "application/json")
    public ResultResponse getDeptList(
                                    @RequestParam(value = "deptNm", required = false) String deptNm) {

        logger.debug(" deptNm:{}", deptNm);

        return deptService.findAllCanCheck(deptNm);
    }

    @GetMapping("/dept/getDeptDetail/{deptId}")
    @ApiOperation(value = "部门详情", notes = "部门详情", produces = "application/json")
    public ResultResponse getDeptDetail(@PathVariable("deptId") Long deptId){
        logger.debug("deptId:{}",deptId);
        return deptService.findDeptByDeptId(deptId);
    }

    @PostMapping("/dept/saveDept")
    @ApiOperation(value = "添加部门列表", notes = "添加部门列表", produces = "application/json")
    public ResultResponse saveDept(@RequestBody SaveDeptRequest saveDeptRequest) {
        logger.debug("saveDeptRequest:{}", saveDeptRequest.toString());
        return deptService.saveDept(saveDeptRequest);

    }

    @PutMapping("/dept/updateDept")
    @ApiOperation(value = "修改部门列表", notes = "修改部门列表", produces = "application/json")
    public ResultResponse updateDept(@RequestBody UpdateDeptRequest updateDeptRequest) {
        logger.debug("updateDeptRequest:{}", updateDeptRequest.toString());
        return deptService.updateDept(updateDeptRequest);
    }

    @DeleteMapping("/dept/delDept/{deptId}")
    @ApiOperation(value = "删除部门", notes = "删除部门", produces = "application/json")
    public ResultResponse delUser(@PathVariable(value = "deptId") Long deptId) {
        logger.debug("deptId:{}", deptId);
        return deptService.delDept(deptId);
    }

    @PostMapping("/dept/assignPost")
    @ApiOperation(value = "分配岗位", notes = "分配岗位", produces = "application/json")
    public ResultResponse assignDept(@RequestBody SavePostByDidAndPid savePostByDidAndPid) {
        logger.debug("savePostByDidAndPid:{}", savePostByDidAndPid.toString());
        return deptService.savePostByDeptIdAndPostId(savePostByDidAndPid);
    }

    @PostMapping("/dept/assignUser")
    @ApiOperation(value = "分配用户", notes = "分配用户", produces = "application/json")
    public ResultResponse assignUser(@RequestBody SaveUserByDidAndUid saveUserByDidAndUid) {
        logger.debug("savePostByDidAndPid");
        return deptService.saveUserByDeptIdAndUsrId(saveUserByDidAndUid);
    }
    @GetMapping("/dept/getDeptTree/{compId}")
    @ApiOperation(value = "获取部门树", notes = "获取部门树", produces = "application/json")
    public ResultResponse getDeptTree(@PathVariable(value = "compId") Long compId) {
        logger.debug("compId:{}", compId);
        ResultResponse resultResponse = new ResultResponse();
        List<DeptPo> deptTree = deptService.findTree(compId);
        resultResponse.setData(deptTree);
        return resultResponse;
    }






}
